// 写路由守卫和路由跳转的文件
// import { ItemChildrenWrap } from 'antd-mobile/es/components/dropdown/item';
import React, { PureComponent } from 'react';
//, withRouter
import { Redirect, Route } from 'react-router-dom';
import { connect } from 'react-redux';
// import Main from '../pages/main';
class RouterView extends PureComponent {
    render() {
        console.log(this.props);
        const { routes, location,token } = this.props;
        //当前页面链接
        const { pathname } = location;

        //路由守卫,需要获取 登录状态
        const isLogin = token !== '' ? true : false; //登录状态可以保存到 redux 、缓存中

        //需要找到匹配的路由
        const targetRouterConfig = routes.find(item => {
            return item.path.replace(/\s*/g, "") === pathname ||
                item.name === pathname.substr(0, pathname.lastIndexOf("/"))
        })
        console.log(targetRouterConfig);

        if (targetRouterConfig !== undefined) {
            const { component, path } = targetRouterConfig;
            if (targetRouterConfig && !targetRouterConfig.auth && !isLogin) {
                return <Route path={path} component={component} />
            }

            if (isLogin) {//已经登录
                return <Route path={path} component={component} />
            } else {
                if (targetRouterConfig && targetRouterConfig.auth) {
                    return <Redirect to="/login" />
                }
            }
        } else {
            return <Redirect to="/page404" />
        }
    }
}

export default connect(
    state => ({ token: state.user.token || '' })
)(RouterView);